/*
 * ParameterUtils.java
 *
 * Created on November 27, 2007, 1:27 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package hextd.util;

import java.applet.Applet;

/**
 *
 * @author hylke
 */
public class ParameterUtils {

	public static int forceIndexBounds(Object[] arr, int index) {
		while (index >= arr.length) {
			index -= arr.length;
		}
		while (index < 0) {
			index += arr.length;
		}
		return index;
	}

	public static int forceIndexBounds(int[] arr, int index) {
		while (index >= arr.length) {
			index -= arr.length;
		}
		while (index < 0) {
			index += arr.length;
		}
		return index;
	}

	public static String paramToUrl(Applet source, String paramName, String deflt) {
		String param = null;
		try {
			param = source.getParameter(paramName);
		} catch (NullPointerException e) {
			//System.err.println("ParameterUtils::paramToUrl: NullPointerException, used in constructor?" + e.getStackTrace());
		}
		String base_url = "";
		if (param != null) {
			if (param.indexOf("http://") == 0) {
				base_url = param;
			} else {
				base_url = source.getCodeBase().toString() + param;
			}
		} else {
			base_url = deflt;
		}
		return base_url;
	}

	public static boolean paramToBool(Applet source, String paramName, boolean deflt) {
		String param = null;
		try {
			param = source.getParameter(paramName);
		} catch (NullPointerException e) {
			//System.err.println("ParameterUtils::paramToBool: NullPointerException, used in constructor?" + e.getStackTrace());
		}

		if (param != null) {
			if (param.toLowerCase().matches("(.*true.*|.*1.*|.*yes.*|.*on.*)")) {
				return true;
			} else if (param.toLowerCase().matches("(.*false.*|.*0.*|.*no.*|.*off.*)")) {
				return false;
			}
		}
		return deflt;
	}

	public static double paramToDouble(Applet source, String paramName, double deflt) {
		String param = null;
		try {
			param = source.getParameter(paramName);
		} catch (NullPointerException e) {
			//System.err.println("ParameterUtils::paramToDouble: NullPointerException, used in constructor?" + e.getStackTrace());
		}
		double returnvalue;

		if (param != null && !param.equalsIgnoreCase("")) {
			try {
				Double newParam = new Double(param);
				returnvalue = newParam.doubleValue();
			} catch (NumberFormatException e) {
				returnvalue = deflt;
			}
		} else {
			returnvalue = deflt;
		}
		return returnvalue;
	}

	public static String paramToString(Applet source, String paramName, String deflt) {
		String param = null;
		try {
			param = source.getParameter(paramName);
		} catch (NullPointerException e) {
			//System.err.println("ParameterUtils::paramToString: NullPointerException, used in constructor?" + e.getStackTrace());
		}
		String returnvalue;

		if (param != null && !param.equalsIgnoreCase("")) {
			returnvalue = param;
		} else {
			returnvalue = deflt;
		}
		return returnvalue;
	}

	public static int paramToInt(Applet source, String paramName, int deflt) {
		String param = null;
		try {
			param = source.getParameter(paramName);
		} catch (NullPointerException e) {
			//System.err.println("ParameterUtils::paramToInt: NullPointerException, used in constructor?" + e.getStackTrace());
		}

		int returnvalue;
		if (param != null && !param.equalsIgnoreCase("")) {
			try {
				Integer newParam = new Integer(param);
				returnvalue = newParam.intValue();
			} catch (NumberFormatException e) {
				returnvalue = deflt;
			}
		} else {
			returnvalue = deflt;
		}
		return returnvalue;
	}

	public static int StringToIntDefault(String in, int deflt) {
		int out;
		try {
			out = new Integer(in).intValue();
		} catch (NumberFormatException e) {
			out = deflt;
		}
		return out;
	}
}
